package com.ambercrm.business.location;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.help.Tip;
import com.ambercrm.R;
import com.ambercrm.base.BaseActivity;
import com.ambercrm.business.location.adapter.SearchAddressListAdapter;
import com.ambercrm.business.location.adapter.SearchTipListAdapter;
import com.ambercrm.business.location.bean.NearAddressBean;
import com.ambercrm.business.location.event.SearchResultEvent;
import com.ambercrm.business.location.event.SearchTipsResultEvent;
import com.ambercrm.business.location.event.StringEvent;
import com.ambercrm.custom_view.ClearEditText;
import com.ambercrm.tools.EmptyUtils;

import java.util.List;

import de.greenrobot.event.EventBus;

public class SearchAddressActivity extends BaseActivity {
    RecyclerView mRecyclerView;
    ClearEditText clearEditText;
    TextView mNoResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_address);
        changeStatusColor(R.color.white);
        mRecyclerView = findViewById(R.id.rv_search);
        clearEditText = findViewById(R.id.et_serach);
        mNoResult = findViewById(R.id.tv_no_result);
        clearEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                EventBus.getDefault().post(new StringEvent(s.toString().trim()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void onEventMainThread(SearchTipsResultEvent event) {
        List<Tip> tipsList = event.getTipList();

        if (!EmptyUtils.isEmpty(tipsList)) {
            mNoResult.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            layoutManager.setOrientation(RecyclerView.VERTICAL);
            mRecyclerView.setLayoutManager(layoutManager);
            SearchTipListAdapter addressListAdapter = new SearchTipListAdapter(this, tipsList);
            mRecyclerView.setAdapter(addressListAdapter);
            addressListAdapter.setOnItemclickListener(new SearchTipListAdapter.OnItemClickListener() {
                @Override
                public void itemClick(Tip tip, int position) {
                    hideSoftKeyBoard(clearEditText);
                    Intent it = new Intent();
                    it.putExtra("tip", tip);
                    setResult(20, it);
                    finish();
                }
            });
        } else {
            mNoResult.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }
    }

    public void onCancelButtonClick(View v) {
        hideSoftKeyBoard(clearEditText);
        finish();
    }
}
